﻿Imports OpenArchive.System.Configuration

Namespace OpenArchive
    Namespace Collections
        Public Class Archives
            Inherits Generic.List(Of Archive)
            Friend Const c_VolumesDir As String = "Volumes"
            Friend Const c_ReadCacheDir As String = "ReadCache"
            Friend Const c_WriteCacheDir As String = "WriteCache"
            Friend Sub New()
                ' Read All Archivsettings
                Dim _archsyssection As ArchiveConfigurationSection
                _archsyssection = System.ArchiveSystem.Settings.Sections(System.ArchiveSystem.c_ArchivConfigSectionName)
                For Each archivconfigelement As ArchiveConfigElement In _archsyssection.Archives
                    Dim _newArchive As New Archive(archivconfigelement.Name, archivconfigelement.Location)
                    MyBase.Add(_newArchive)
                Next
            End Sub
            Public Shadows Sub Add(ByVal Archive As Archive)
                ' Get StdLocatoin of System
                Dim _archsyssection As ArchiveConfigurationSection
                Dim _StdLocation As String
                _archsyssection = System.ArchiveSystem.Settings.Sections(System.ArchiveSystem.c_ArchivConfigSectionName)
                If Not _archsyssection Is Nothing Then
                    _StdLocation = _archsyssection.Systemsettings(System.ArchiveSystem.c_StdLocation).Value
                    Me.Add(Archive, _StdLocation)
                Else
                    Throw New Exception("System not initialized")
                End If
            End Sub
            Public Shadows Sub Add(ByVal Archive As Archive, ByVal Location As String)
                Dim _archsyssection As ArchiveConfigurationSection
                Dim _dirsep As String = Global.System.IO.Path.DirectorySeparatorChar
                Archive.Location = Location & _dirsep & Archive.Name
                Try
                    _archsyssection = System.ArchiveSystem.Settings.Sections(System.ArchiveSystem.c_ArchivConfigSectionName)
                    If Not _archsyssection Is Nothing Then
                        If _archsyssection.Archives.Item(Archive.Name) Is Nothing Then
                            Dim _archivconfigelement As ArchiveConfigElement = New ArchiveConfigElement(Archive.Name, Archive.Location)
                            _archsyssection.Archives.Add(_archivconfigelement)
                        Else
                            Throw New ArgumentException("Adding duplicate")
                        End If
                        System.ArchiveSystem.Settings.Save()
                    Else
                        Throw New Exception("System not initialized")
                    End If
                Catch ex As Exception
                    Throw ex
                End Try

                If Not Global.System.IO.Directory.Exists(Archive.Location) Then
                    Dim _StdArchiveSettings As ArchiveSettings = New ArchiveSettings()
                    _StdArchiveSettings.ArchiveImmediately = True
                    _StdArchiveSettings.ArchiveACG = "testGroup"
                    CreateArchive(Archive, _StdArchiveSettings, Location & Global.System.IO.Path.DirectorySeparatorChar & Archive.Name)
                End If
                MyBase.Add(Archive)
            End Sub
            Default Public Shadows ReadOnly Property Item(ByVal Name As String) As Archive
                Get
                    For Each _archive As Archive In Me
                        If _archive.Name = Name Then Return _archive
                    Next
                    Return Nothing
                End Get
            End Property

            Private Sub CreateArchive(ByVal Archive As Archive, ByVal ArchiveSettings As ArchiveSettings, ByVal Location As String)
                'Dim _Archivedirectory As System.IO.Directory ' = New System.IO.Directory()

                If Global.System.IO.Directory.Exists(Location) Then
                    Throw New ArgumentException("Adding duplicate")
                End If
                Global.System.IO.Directory.CreateDirectory(Location)
                CreateSubDirectories(Archive, ArchiveSettings, Location)
                CreateTables(Archive, ArchiveSettings, Location)
                System.Utils.CreateAccessGroup(ArchiveSettings.ArchiveACG)
                Archive.Settings = ArchiveSettings
            End Sub
            Private Shared Sub CreateTables(ByVal Archive As Archive, ByVal ArchiveSettings As ArchiveSettings, ByVal Location As String)
                Archive.DataProviderInstance.CreateTables(Archive.Name, ArchiveSettings, Location)
            End Sub
            Private Shared Sub CreateSubDirectories(ByVal Archive As Archive, ByVal ArchiveSettings As ArchiveSettings, ByVal ArchiveLocation As String)
                Dim _dirsep As Char = Global.System.IO.Path.DirectorySeparatorChar
                System.Utils.CreateDirectory(ArchiveLocation & _dirsep & c_VolumesDir)
                System.Utils.CreateDirectory(ArchiveLocation & _dirsep & c_ReadCacheDir)
                System.Utils.CreateDirectory(ArchiveLocation & _dirsep & c_WriteCacheDir)
            End Sub

        End Class
    End Namespace
End Namespace
